package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TPurchase;
import com.ruoyi.system.service.IBdMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TReceiveMapper;
import com.ruoyi.system.domain.TReceive;
import com.ruoyi.system.service.ITReceiveService;

/**
 * 物资领用Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-22
 */
@Service
public class TReceiveServiceImpl implements ITReceiveService 
{
    @Autowired
    private TReceiveMapper tReceiveMapper;

    @Autowired
    private IBdMaterialService iBdMaterialService;
    /**
     * 查询物资领用
     * 
     * @param id 物资领用主键
     * @return 物资领用
     */
    @Override
    @DataSource(value = DataSourceType.MASTER)
    public int selectTReceiveById(String id)
    {
        TReceive tReceive = tReceiveMapper.selectTReceiveById(id);
        tReceive.setReceiveStatus(2);
        return tReceiveMapper.updateTReceive(tReceive);
    }

    /**
     * 查询物资领用列表
     * 
     * @param tReceive 物资领用
     * @return 物资领用
     */
    @Override
    @DataSource(value = DataSourceType.MASTER)
    public List<TReceive> selectTReceiveList(TReceive tReceive)
    {
        List<TReceive> tReceives = tReceiveMapper.selectTReceiveList(tReceive);
        for (TReceive receive : tReceives) {
            //去用友查询单据的状态
            Integer status = iBdMaterialService.selectTPurchaseStatus(receive.getId());
            receive.setReceiveStatus(status);
        }
        return tReceives;
    }

    /**
     * 新增物资领用
     * 
     * @param tReceive 物资领用
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.MASTER)
    public int insertTReceive(TReceive tReceive)
    {
        tReceive.setCreateTime(DateUtils.getNowDate());
        return tReceiveMapper.insertTReceive(tReceive);
    }

    /**
     * 修改物资领用
     * 
     * @param tReceive 物资领用
     * @return 结果
     */
    @Override
    public int updateTReceive(TReceive tReceive)
    {
        return tReceiveMapper.updateTReceive(tReceive);
    }

    /**
     * 批量删除物资领用
     * 
     * @param ids 需要删除的物资领用主键
     * @return 结果
     */
    @Override
    public int deleteTReceiveByIds(String[] ids)
    {
        return tReceiveMapper.deleteTReceiveByIds(ids);
    }

    /**
     * 删除物资领用信息
     * 
     * @param id 物资领用主键
     * @return 结果
     */
    @Override
    public int deleteTReceiveById(String id)
    {
        return tReceiveMapper.deleteTReceiveById(id);
    }

    @Override
    @DataSource(value = DataSourceType.MASTER)
    public BigDecimal getDisburseTotal(String pkProjectclass) {
        return tReceiveMapper.getDisburseTotal(pkProjectclass);
    }
}
